﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
namespace QuanLyDiemHocSinh
{
    public partial class Doimatkhau : System.Web.UI.Page
    {
        String tendn, matkhau;
        protected void Page_Load(object sender, EventArgs e)
        {
            tendn = Session["tendn"].ToString();
        }

        protected void btmatkhau_Click(object sender, EventArgs e)
        {
            string chuoiketnoi = "Server=.\\SqlExpress;Integrated Security=True;Database=QuanLyDiem";
            SqlConnection ketnoi = new SqlConnection(chuoiketnoi);

            SqlDataReader rdr = null;
            SqlCommand cmd = new SqlCommand("select MatKhau from NguoiDung where (MaNguoiDung= @tendn )", ketnoi);
            cmd.Parameters.AddWithValue("@tendn", tendn);
            try
            {
                ketnoi.Open();

                rdr = cmd.ExecuteReader();
                rdr.Read();
                //while (rdr.Read())
                //{
                //tendn = (string)rdr["MaNguoiDung"];
                //Lay mat khau tu ten dang nhap
                String matkhau = (string)rdr["MatKhau"];

                if (string.Compare(matkhau, tbmkcu.Text) == 0)
                {
                    if (string.Compare(tbmkmoi.Text, tbmklaplai.Text) == 0)
                    {//Tien hanh doi mat khau
                        //Session["tendn"] = tendn;
                        //Session["quyendn"] = quyen;
                        //Response.Redirect("~/Admin.aspx");
                        rdr.Close();
                        SqlCommand cmd1 = new SqlCommand("update NguoiDung set Matkhau = @matkhaumoi where MaNguoiDung=@tendn", ketnoi);
                        cmd1.Parameters.AddWithValue("@tendn", tendn);
                        cmd1.Parameters.AddWithValue("@matkhaumoi", tbmkmoi.Text);

                        cmd1.ExecuteNonQuery();
                        lbketnoi.Text = "Đổi mật khẩu thành công !";
                        // break;
                    }
                    else lbketnoi.Text = "Mật khẩu mới và cũ không trùng nhau";
                }
                else lbketnoi.Text = "Sai mật khẩu cũ !!!!";
            }
            //}
            catch (Exception ex)
            {
                lbketnoi.Text = ex.ToString();
            }

        }
    }
}